package com.wxc.impl;


import com.wxc.MyMap;
import com.wxc.MySet;
import com.wxc.rbtree.IComparator;

/**
 * @author wxc
 * @date 2021年08月16日 8:57 PM
 * 不能添加null
 */
public class TreeSet<E> implements MySet<E> {

	TreeMap<E, Object> treeMap;

	public TreeSet() {
		this.treeMap = new TreeMap<>();
	}

	public TreeSet(IComparator<E> comparator) {
		this.treeMap = new TreeMap<>(comparator);
	}

	@Override
	public int size() {
		return treeMap.size();
	}

	@Override
	public boolean isEmpty() {
		return treeMap.isEmpty();
	}

	@Override
	public void clear() {
		treeMap.clear();
	}

	@Override
	public boolean contains(E element) {
		return treeMap.containsKey(element);
	}

	@Override
	public void add(E element) {
		// 默认是去重的
		treeMap.put(element, null);
	}

	@Override
	public void remove(E element) {
		treeMap.remove(element);
	}

	@Override
	public void traversal(Visitor<E> visitor) {
		treeMap.traversal(new MyMap.Visitor<E, Object>() {
			@Override
			public boolean visit(E key, Object value) {
				return visitor.visit(key);
			}
		});
	}
}
